<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px" @submit.native.prevent>
<!--    <el-form-item label="用户昵称" prop="nickName">-->
<!--      <el-input v-model="user.nickName" />-->
<!--    </el-form-item>-->
    <el-form-item label="手机号码" prop="phonenumber">
      <el-input v-model="user.phonenumber" maxlength="11" />
    </el-form-item>
    <el-form-item label="微信" prop="wechat">
      <el-input v-model="user.wechat" maxlength="50" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="user.email" maxlength="50" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="user.sex">
        <el-radio label="0">男</el-radio>
        <el-radio label="1">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="我的首页">
      <treeselect
        v-model="user.homeMenu"
        :options="menuOptions"
        :multiple="false"
        :max-height="200"
        :show-count="true"
        :disable-branch-nodes="true"
        appendToBody
        placeholder="请选择"
      />
    </el-form-item>

    <el-form-item label="列表条目">
      <el-input v-model="user.pageSize" />
    </el-form-item>

    <el-form-item label="快捷命令">
      <treeselect
        v-model="quickMenu"
        :options="menuOptions"
        :multiple="true"
        :show-count="true"
        :max-height="200"
        appendToBody
        placeholder="请选择"
        :disable-branch-nodes="true"
        @input="handleInput"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { updateUserProfile } from "@/api/system/user";
import {treeselect as menuTreeselect} from "@/api/system/menu";

export default {
  props: {
    user: {
      type: Object
    }
  },
  components: {
    Treeselect
  },
  data() {
    return {
      // 表单校验
      rules: {
        // nickName: [
        //   { required: true, message: "用户昵称不能为空", trigger: "blur" }
        // ],
        email: [
          // { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          // { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      quickMenu:[],
      // 菜单列表
      menuOptions: [],
      flag:true
    };
  },
  created() {
    // this.getMenuTreeselect();
  },
  watch: {
    async user(val){
      if(this.flag){
        this.quickMenu=[]
        for(let item of val.quickMenu.split(',')){
          this.quickMenu.push(Number(item))
        }
        await this.getMenuTreeselect();
        this.flag=false
      }
    },
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserProfile(this.user).then(response => {
            if (response.code === 200) {
              this.msgSuccess("修改成功");
            }
          });
        }
      });
    },
    /** 查询菜单树结构 */
    getMenuTreeselect() {
      menuTreeselect().then(response => {
        this.menuOptions = this.menuOptions.concat(response.data);
      });
    },
    close() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    },
    handleInput() {
      //限制多选条数
      if (this.quickMenu.length > 5) {
        this.$message.warning("最多可选择5条数据");
        this.quickMenu.pop();
      }
      this.user.quickMenu = this.quickMenu.join(',')
    },
  }
};
</script>
<style scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
  height: auto;
  max-height: 274px;
  padding: 0;
  overflow: hidden;
}
.el-select-dropdown__item.selected{
  font-weight: normal;
}
ul li >>>.el-tree .el-tree-node__content{
  height:auto;
  padding: 0 20px;
}
.el-tree-node__label{
  font-weight: normal;
}
.el-tree >>>.is-current .el-tree-node__label{
  color: #409EFF;
  font-weight: 700;
}
.el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
  color:#606266;
  font-weight: normal;
}
</style>
